package com.gnerv.author.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gnerv.author.entity.User;
import com.gnerv.author.mapper.UserMapper;
import com.gnerv.author.service.IUserService;
import com.gnerv.author.util.PasswordUtil;
import com.gnerv.author.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 基础平台-用户表 服务实现类
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-07-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public boolean createUser(User user) {
        user.setbId(UUIDUtil.getLower());
        user.setPassword(PasswordUtil.getEncryptString(user));
        userMapper.insert(user);
        return true;
    }

    @Override
    public boolean deleteUser(String bId) {
        userMapper.delete(new QueryWrapper<>(new User(bId)));
        return true;
    }

    @Override
    public boolean updateUser(User user) {
        user.setPassword(null);
        userMapper.update(user, new QueryWrapper<>(new User(user.getbId())));
        return true;
    }

    @Override
    public User selectUser(String bId) {
        User user = userMapper.selectOne(new QueryWrapper<>(new User(bId)));
        return user;
    }

    @Override
    public User selectUser(User user) {
        user = userMapper.selectOne(new QueryWrapper<>(user));
        return user;
    }

    @Override
    public List<User> listUser(User user) {
        List<User> users = userMapper.selectList(new QueryWrapper<>(user));
        return users;
    }
}
